<style>
.table > thead > tr > th {padding:15px 10px;font-size:18px;}
.table > tbody > tr > td {padding:10px 10px;vertical-align: middle;}
.cart-iten-list img{max-width:90px;max-height:90px;}
.ui-widget-header, .table thead, div.dataTables_wrapper {
    background: #ddd; color: #333;
}
</style>
<script>
var setAmount={
		min:1,
		max:9999,
		reg:function(x){
			return new RegExp("^[1-9]\\d*$").test(x);
		},
		amount:function(obj,mode){
			var x=$(obj).val();
			if (this.reg(x)){
				if (mode){
					x++;
				}else{
					x--;
				}
			}else{
				alert("请输入正确的数量！");
				$(obj).val(1);
				$(obj).focus();
			}			
			return x;
		},
		reduce:function(obj){
			var x=this.amount(obj,false);
			if (x>=this.min){
				$(obj).val(x);
			}else{
				alert("商品数量最少为"+this.min);
				$(obj).val(this.min);
				$(obj).focus();
			}
			editCartNum($(obj).data('id'),$(obj).val());
		},
		add:function(obj){
			var x=this.amount(obj,true);
			if (x<=this.max){
				$(obj).val(x);
			}else{
				alert("商品数量最多为"+this.max);
				$(obj).val(this.max);
				$(obj).focus();
			}
			editCartNum($(obj).data('id'),$(obj).val());
		},
		modify:function(obj){
			var x=$(obj).val();
			if (x<this.min||x>this.max||!this.reg(x)){
				alert("请输入正确的数量！");
				$(obj).val(this.min);
				$(obj).focus();
			}
		}
}
var last_hash = randomString(10);
/* 设置一个hash值校验，防止联系多次点击时，返回的先后顺序不对覆盖成过期数据了 */
var refreshCartContainer = function(request,moreags){
	if(request.hash == last_hash) {
		var html = '<div>' + request.carts_html + '</div>';
		var newhtml = $(html).find('#cart-list-container').html();
		$('#cart-list-container').html(newhtml);
	}
	else{
		// hash 校验数据不对的直接抛弃
	}
}
function editCartNum(id,num){
	last_hash = randomString(10);
	var url=BASEURL+'/carts/editCartNum/'+id+'/'+num+'?hash='+last_hash;	
	// var postdata = {'data[Cart][num]':num,'data[Cart][product_id]':id};
	if(!sso.check_userlogin({"callback":editCartNum,"callback_args":arguments}))
		return false;
	ajaxAction(url,null,null,refreshCartContainer);
	return false;
}

$(function(){
	
	$(document).on('keyup','.cart-num-input',function(){
		// @todo 检查为字母符号等键时跳过。只支持数字，剪切，粘贴,回退删除		
		if($(this).val() > 0 ) {
			editCartNum($(this).data('id'),$(this).val());
		}		
	});
})

function deleteCartItem(id){
	last_hash = randomString(10);
	var url=BASEURL+'/carts/delete/'+id+'?hash='+last_hash;	
	// var postdata = {'data[Cart][num]':num,'data[Cart][product_id]':id};
	if(!sso.check_userlogin({"callback":deleteCartItem,"callback_args":arguments}))
		return false;
	
	ajaxAction(url,null,null,refreshCartContainer);
	return false;
}


</script>
<div  id="cart-list-container"  class="container">
<portlet portlet="notitle">
<form action="{{$this->Html->url('/orders/info')}}" method="post">

<table class="table cart-iten-list cart-list table-hover">
<thead>
<tr><th><input type="checkbox" checked="checked" id="check-all"></th><th width="40%" class="text-center">名称</th><th>图片</th><th>价格</th><th>数量</th><th class="text-right">合计</th><th></th></tr></thead>
{{loop $Carts $cart}}
<tr>
<th><input name="data[Cart][{{$cart['Cart']['id']}}]" checked="checked" value="1"  type="checkbox"></th>
<td><a href="{{Router::url('/'.Inflector::tableize($cart['Cart']['product_model']).'/view/'.$cart['Cart']['product_id'])}}" target="_blank">{{$cart['Cart']['name']}}</a></td>
<td>{{if $cart['Cart']['coverimg']}}<a href="{{Router::url('/'.Inflector::tableize($cart['Cart']['product_model']).'/view/'.$cart['Cart']['product_id'])}}" target="_blank">{{$this->Html->image(Router::url($cart['Cart']['coverimg']))}}</a>{{/if}}</td>
<td><span class=" product-price"><small>{{format_price($cart['Cart']['price'])}}</small></span></td>
<td>
<div class="input-group" style="float:left;width:135px;">
<span class="reduce input-group-addon" onclick="setAmount.reduce('#pamount-{{$cart['Cart']['id']}}')">-</span>
<input type="number" size="4" id="pamount-{{$cart['Cart']['id']}}" data-id="{{$cart['Cart']['id']}}" data-price="{{$cart['Cart']['price']}}" class="form-control cart-num-input" name="data[Cart][{{$cart['Cart']['id']}}][num]" value="{{$cart['Cart']['num']}}" />
<span class="add input-group-addon" onclick="setAmount.add('#pamount-{{$cart['Cart']['id']}}')">+</span>
</div>
</td>
<td class="text-right"><span class="item-price product-price" id="price-{{$cart['Cart']['id']}}"><small>{{format_price($cart['Cart']['price']*$cart['Cart']['num'])}}</small></span></td>
<td><a href="javascript:void(0)" data-url="{{Router::url('/carts/delete/'.$cart['Cart']['id'])}}" onclick="deleteCartItem({{$cart['Cart']['id']}})"><i class="fa fa-trash trash-item"></i></a></td>
</tr>
{{/loop}}

</table>

<div class="text-right" style="margin-right:20px;margin-top:30px;">
{{if $ship_fee}}
<p>总价：<span id="total-price">{{format_price($total_price)}}</span></p>
<p>快递费用：<span id="total-price">{{format_price($ship_fee)}}</span></p>
{{/if}}
<p>合计：<span id="total-price" class="product-price">{{format_price($total_price + $ship_fee)}}</span></p>
<p><input style="padding:8px 35px;" class="btn btn-lg btn-warning" type="submit"  value="结算" /></p>
</div>

</form>
</portlet>
</div>
